package com.nanhai.buddhist.college.module.study.controller.admin.attendanceleavereason;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.error;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.NOT_ALLOW_DELETE;

import com.nanhai.buddhist.college.module.study.controller.admin.attendanceleavereason.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendanceleavereason.AttendanceLeaveReasonDO;
import com.nanhai.buddhist.college.module.study.convert.attendanceleavereason.AttendanceLeaveReasonConvert;
import com.nanhai.buddhist.college.module.study.service.attendanceleavereason.AttendanceLeaveReasonService;

@Api(tags = "管理后台 - 请假事由")
@RestController
@RequestMapping("/study/attendance-leave-reason")
@Validated
public class AttendanceLeaveReasonController {

    @Resource
    private AttendanceLeaveReasonService attendanceLeaveReasonService;

    @PostMapping("/create")
    @ApiOperation("创建请假事由")
    @PreAuthorize("@ss.hasPermission('study:attendance-leave-reason:create')")
    public CommonResult<String> createAttendanceLeaveReason(@Valid @RequestBody AttendanceLeaveReasonCreateReqVO createReqVO) {
        return success(attendanceLeaveReasonService.createAttendanceLeaveReason(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新请假事由")
    @PreAuthorize("@ss.hasPermission('study:attendance-leave-reason:update')")
    public CommonResult<Boolean> updateAttendanceLeaveReason(@Valid @RequestBody AttendanceLeaveReasonUpdateReqVO updateReqVO) {
        attendanceLeaveReasonService.updateAttendanceLeaveReason(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除请假事由")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('study:attendance-leave-reason:delete')")
    public CommonResult<Boolean> deleteAttendanceLeaveReason(@RequestParam("id") String id) {
        Boolean delSuccess = attendanceLeaveReasonService.deleteAttendanceLeaveReason(id);
        if (delSuccess) {
            return success(true);
        }
        else{
            return error(NOT_ALLOW_DELETE);
        }
    }

    @GetMapping("/get")
    @ApiOperation("获得请假事由")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('study:attendance-leave-reason:query')")
    public CommonResult<AttendanceLeaveReasonRespVO> getAttendanceLeaveReason(@RequestParam("id") String id) {
        AttendanceLeaveReasonDO attendanceLeaveReason = attendanceLeaveReasonService.getAttendanceLeaveReason(id);
        return success(AttendanceLeaveReasonConvert.INSTANCE.convert(attendanceLeaveReason));
    }

    @GetMapping("/list")
    @ApiOperation("获得请假事由列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('study:attendance-leave-reason:query')")
    public CommonResult<List<AttendanceLeaveReasonRespVO>> getAttendanceLeaveReasonList(@RequestParam("ids") Collection<String> ids) {
        List<AttendanceLeaveReasonDO> list = attendanceLeaveReasonService.getAttendanceLeaveReasonList(ids);
        return success(AttendanceLeaveReasonConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得请假事由分页")
    @PreAuthorize("@ss.hasPermission('study:attendance-leave-reason:query')")
    public CommonResult<PageResult<AttendanceLeaveReasonRespVO>> getAttendanceLeaveReasonPage(@Valid AttendanceLeaveReasonPageReqVO pageVO) {
        PageResult<AttendanceLeaveReasonDO> pageResult = attendanceLeaveReasonService.getAttendanceLeaveReasonPage(pageVO);
        return success(AttendanceLeaveReasonConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出请假事由 Excel")
    @PreAuthorize("@ss.hasPermission('study:attendance-leave-reason:export')")
    @OperateLog(type = EXPORT)
    public void exportAttendanceLeaveReasonExcel(@Valid AttendanceLeaveReasonExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AttendanceLeaveReasonDO> list = attendanceLeaveReasonService.getAttendanceLeaveReasonList(exportReqVO);
        // 导出 Excel
        List<AttendanceLeaveReasonExcelVO> datas = AttendanceLeaveReasonConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "请假事由.xls", "数据", AttendanceLeaveReasonExcelVO.class, datas);
    }

}
